/***********************************************
 This file is part of redia-lib-utils-ios
.
 
 Copyright (c) 2012, 2013 Redia A/S
 
 Redia-library-app-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-library-app-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-library-app-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */


#import "InfoGalleriImageUrlUtils.h"
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
//For legacy support we use TBXML's base64Encoding implementation when targeting devices running iOS <7
#import "TBXML+Compression.h"
#endif
#import "defines.h"

@implementation InfoGalleriImageUrlUtils

@synthesize wsApiKey;
@synthesize screenScale;

static InfoGalleriImageUrlUtils *sharedSingleton;

+ (void)initialize
{
    static BOOL initialized = NO;
    if(!initialized)
    {
        initialized = YES;
        sharedSingleton = [[InfoGalleriImageUrlUtils alloc] init];
        sharedSingleton.wsApiKey = @"";
        sharedSingleton.screenScale = SCREEN_SCALE;
    }
}

+ (InfoGalleriImageUrlUtils*)instance
{
	return sharedSingleton;
}


+ (NSString*)getResizedImageUrl:(NSString*)url toWidth:(NSInteger)width toHeight:(NSInteger)height usingQuality:(NSInteger)quality
{
	return [InfoGalleriImageUrlUtils getResizedImageUrl:url toWidth:width toHeight:height usingQuality:quality withMode:IMAGE_URL_UTILS_RESIZE_MODE_BOUNDING_BOX outputFormat:nil doubleUpForRetina:NO];
}

+ (NSString*)getResizedImageUrl:(NSString*)url toWidth:(NSInteger)width toHeight:(NSInteger)height usingQuality:(NSInteger)quality withMode:(IMAGE_URL_UTILS_RESIZE_MODE)mode
{
    return [InfoGalleriImageUrlUtils getResizedImageUrl:url toWidth:width toHeight:height usingQuality:quality withMode:mode outputFormat:nil doubleUpForRetina:NO];
}

+ (NSString*)getResizedImageUrl:(NSString*)url toWidth:(NSInteger)width toHeight:(NSInteger)height usingQuality:(NSInteger)quality withMode:(IMAGE_URL_UTILS_RESIZE_MODE)mode outputFormat:(NSString*)output_format
{
    return [InfoGalleriImageUrlUtils getResizedImageUrl:url toWidth:width toHeight:height usingQuality:quality withMode:mode outputFormat:nil doubleUpForRetina:NO];
}

+ (NSString *)getResizedImageUrl:(NSString *)url toWidth:(NSInteger)width toHeight:(NSInteger)height usingQuality:(NSInteger)quality withMode:(IMAGE_URL_UTILS_RESIZE_MODE)mode outputFormat:(NSString *)output_format doubleUpForRetina:(BOOL)check_retina
{
	NSMutableString* res = [NSMutableString stringWithString:REDIA_APP_IMAGE_RESCALING_SERVICE_URL];

	if (mode == IMAGE_URL_UTILS_RESIZE_MODE_CROP_EDGES) {
		[res appendString:@"crop"];
	} else {
		//bounding box
		[res appendString:@"bb"];
	}
    
    if (check_retina) {
        CGFloat scaling = [self instance].screenScale;
        height *= scaling;
        width *= scaling;
    }

    NSString* apikey = [InfoGalleriImageUrlUtils instance].wsApiKey;
    
    if (apikey==nil || [apikey length]==0) {
        NSLog(@"ERROR! NO API KEY SET FOR InfoGalleriImageUrlUtils");
    }
	
    /* old code, too general
     NSURL *urlObject = [NSURL URLWithString:url];
     url = [urlObject absoluteStringAddingParam:@"e" value:@"http"];
     */
    if ([url hasPrefix:@"https://publish-media.redia.dk"] || [url hasPrefix:@"http://publish-media.dev.redia.dk"]) {
        //add failover parameter to http errors for in-house media urls
        url = [url stringByAppendingString:@"&e=http"];
    }
    
	NSData* url_utf8data = [url dataUsingEncoding:NSUTF8StringEncoding];
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_7_0
    NSString* encoded_url = [url_utf8data base64EncodedStringWithOptions:0];
#else
    //For legacy support we use TBXML's base64Encoding implementation when targeting devices running iOS <7
    NSString* encoded_url = [url_utf8data base64Encoding];
#endif
	
	
	[res appendFormat:@"&u=%@&w=%ld&h=%ld&q=%ld&e=http&a=%@",encoded_url,(long)width,(long)height,(long)quality,apikey];
    if (output_format!=nil) {
        [res appendFormat:@"&o=%@",output_format];
    }
	//DLog(@"getResizedImageUrl: %@",res);
	return res;
	
}

@end
